package com.aigou.admin.controller;

import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.utils.UUIDUtils;
import com.aigou.admin.entity.CarouselFigure;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.vo.ResponseVo;
import com.aigou.admin.service.CarouselFigureService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: Hor
 * @Date: 2024/12/3 16:54
 * @Version: 1.0
 */

@RestController
@RequestMapping("/carouse")
@AllArgsConstructor
@ResponseResult
public class CarouselFigureController {

    private final CarouselFigureService carouselFigureService;

    //将方法名称作为参数传递  m=save
    @RequestMapping("/save")
    public ResponseVo save(HttpServletRequest req, HttpServletResponse resp) {
        String url = req.getParameter("url");
        Integer sequenceNum = Integer.valueOf(req.getParameter("sequence_num"));

        // 生成uuid
        String uuid = UUIDUtils.generateUUIDWithoutHyphens();

        Boolean save = carouselFigureService.save(new CarouselFigure(uuid, url, sequenceNum));
        return ResponseVo.buildSuccess(save);
    }

    @RequestMapping("/update")
    public ResponseVo update(HttpServletRequest req, HttpServletResponse resp) {
        String id =req.getParameter("id");
        String url = req.getParameter("url");
        Integer sequenceNum = Integer.valueOf(req.getParameter("sequence_num"));
        Boolean update = carouselFigureService.update(new CarouselFigure(id, url, sequenceNum));
        return ResponseVo.buildSuccess(update);
    }

    @RequestMapping("/delete")
    public ResponseVo delete(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        boolean rs = carouselFigureService.remove(id);
        return ResponseVo.buildSuccess(rs);
    }

    @RequestMapping("/findById")
    public ResponseVo findById(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        CarouselFigure carouselFigure = carouselFigureService.findById(id);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(carouselFigure);
    }

    @RequestMapping("/findAll")
    public ResponseVo findAll() {
        //查询用户
        List<CarouselFigure> carouselFigures = carouselFigureService.findAll();
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(carouselFigures);
    }

    @RequestMapping("/page")
    public ResponseVo page(String currentPage, String pageSize) {
        //查询用户
        PageEntity<CarouselFigure> carouselFigurePageEntity = carouselFigureService.findCarouselFiguresByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(carouselFigurePageEntity);
    }


}
